package UI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

import Debug.Debug;
import Enums.Type_rsp;
import Interfaces.C_interface;
import Structs.Msg_rsp;
import Threads.C_TCP_in;
import Threads.C_TCP_out;

public class Client_Drop_TCP  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @param args
	 */

	private static String User="";
	private static String requests;
	private static Type_rsp Type = null;
	private static BufferedReader reader;
	private static Msg_rsp newmsg = null;
	private static int id;
	private static BlockingQueue<Msg_rsp> msg = new LinkedBlockingQueue<Msg_rsp>();
	private static boolean logedin=false;

	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		int count=0;



		C_TCP_out out_in=new C_TCP_out(msg,6000,"localhost");

		reader = new BufferedReader(new InputStreamReader(System.in));

		System.out.println("------------------");
		System.out.println("Welcome to MyDrop");
		System.out.println("------------------");
		
		do{

			try{
				while(!(Connect(out_in)));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				if(new Debug().isDebug())
					System.out.println("IO Exception client ui");
				System.out.println("Server Down please try later");
				Type = Type_rsp.Exit;
			}
			logedin=true;
			
			new Thread(out_in).start();
			newmsg=null;
			if(Type != Type_rsp.Exit){
				do{
					try {
						newmsg = AcceptRequest();
						
						if(newmsg!=null){
							msg.put(newmsg);
							Type = newmsg.getType();
						}
					}catch (IOException e) {
						// TODO Auto-generated catch block
						if(new Debug().isDebug())
							System.out.println("IO Exception");
					} catch (IllegalArgumentException e){
						if(new Debug().isDebug())
							System.out.println("Illegal Argument Exception");
						System.out.println("Invalid command!\n for help just write Help");
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						if(new Debug().isDebug())
							System.out.println("Interrupted Exception drop tcp");
					} 

				}while(Type!=Type_rsp.Logout || Type != Type_rsp.Exit);
				logedin=false;
			}

		}while(Type!=Type_rsp.Exit);

		System.out.println("Thx for using MyDrop");
	}

	private static boolean Connect(C_TCP_out out_in) throws IOException{
		int input;
		String password;
		System.out.println("Choose command:\n1.Login\n2.Register\n3.Exit");
		try {
			input = Integer.parseInt(reader.readLine());
			switch(input){
			case 1:
				System.out.println("Insert Username:");
				User = reader.readLine();
				System.out.println("Insert Password:");
				password = reader.readLine();
				if(out_in.tryconnection(User, password, Type_rsp.Login)){
					System.out.println("Welcome!");
					return true;
				}
				System.out.println("Login or password incorrect!");
				return false;
			case 2:
				System.out.println("New Insert Username:");
				User = reader.readLine();
				System.out.println("New Insert Password:");
				password = reader.readLine(); 
				if(out_in.tryconnection(User, password, Type_rsp.Register)){
					System.out.println("Welcome");
					return true;
				}
				System.out.println("User not in System!");
				return false;
			case 3:
				System.out.println("Bye Bye");
				Type = Type_rsp.Exit;
				return true;
			default:
				System.out.println("Incorrect input try again!");
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			if(new Debug().isDebug())
				System.out.println("Number Format Exception");
		} 
		return false;
	}

	private static Msg_rsp AcceptRequest() throws IOException, IllegalArgumentException {
		System.out.println("Select Command:");
		requests = reader.readLine();

		Type = Type_rsp.valueOf(requests);
		if(Type != Type_rsp.Logout || Type != Type_rsp.Exit){
			System.out.println("Select Task:");
		}
		switch (Type) {
		case NewTask:
			System.out.println("Select Name for the task:");
			requests = reader.readLine();
			return new Msg_rsp(Type,requests,0);
		case EditTask:
			System.out.println("Insert ID to edit:");
			id = Integer.parseInt(reader.readLine());
			System.out.println("Insert the new Name:");
			requests = reader.readLine();
			return new Msg_rsp(Type,requests,id);
		case FinishTask:
			System.out.println("Insert ID to terminate:");
			id = Integer.parseInt(reader.readLine());
			return new Msg_rsp(Type,null,id);
		case ListTasks:
			return new Msg_rsp(Type,null,0);
		case Logout:
			return new Msg_rsp(Type,User,0);
		case Exit:
			return new Msg_rsp(Type,User,0);
		default:
			return null;
		} 

	}


}
